package com.weixiaodong.ModelDesign.适应设计模式.Iterator模式.遍历书籍程序案例演示;

/**
 * 继承迭代接口，实现书架的迭代器
 */
@SuppressWarnings("all")
public class BookShelfIterator implements Iterator{

    private BookShelf bookShelf;

    private int index;

    public BookShelfIterator(BookShelf bookShelf) {
        this.bookShelf = bookShelf;
        this.index = 0;
    }

    @Override
    public boolean hasNext() {
        return this.index < this.bookShelf.getSize();
    }

    @Override
    public Book next() {
        if (hasNext()) {
            Book book = this.bookShelf.getBook(this.index);
            this.index++;
            return book;
        }
        return null;
    }
}
